<#--
List of variables to be passed for this template:
        +- rootPackage
        +- applicationName
        +- classNames
        +- directory
        +- Entities: ArrayList of EntityClass.getAsMap()

            |
            +- imports: imports
            |
            +- name: The name of the class.
            |    |
            |    +- single
            |    +- plural
            |
            +- idColumn: the id column
            |    |
            |    +- field
            |    |    |
            |    |    +- name
            |    |    +- type
            |    |
            |    +- getter
            |    +- setter
            |
            +- columns: ArrayList of columns
            |    |
            |    +- field
            |    |    |
            |    |    +- name
            |    |    +- type
            |    |    +- hasTypeParameter
            |    |    +- typeParameter -- only if hasTypeParameter returns true!
            |    +- getter
            |    +- setter

-->
<@pp.dropOutputFile />
<#list Entities as entity>
<@pp.changeOutputFile name="/development/${directory}/apis/rest/converters/${entity.name.single}Converter.java" />

package ${rootPackage}.${applicationName}.apis.rest.converters;

<#list entity.imports as import>
import ${import};
</#list>
<#list entity.columns as column>
    <#if column.field.hasTypeParameter >
        <#list classNames as name>
            <#if name.single == column.field.typeParameter >
import ${rootPackage}.${applicationName}.models.${name.single};
            </#if>
        </#list>
    </#if>
</#list>
import ${rootPackage}.${applicationName}.models.${entity.name.single};
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("${entity.name.single?uncap_first}")
public class ${entity.name.single}Converter {

    public ${entity.name.single}Converter() {
    }

    public ${entity.name.single}Converter(${entity.name.single} entity) {
        if(entity == null)
    		return;
        this.${entity.idColumn.field.name} = entity.${entity.idColumn.getter}();
        <#list entity.columns as column>
            <#list classNames as name>
            <#assign found = false >
            <#if name.single == column.field.type >
                <#assign found = true >
                <#break>
            </#if>
            </#list>
        <#if column.field.hasTypeParameter >
            <#list classNames as name>
                <#assign foundTypeParameter = false >
                <#if name.single == column.field.typeParameter >
                    <#assign foundTypeParameter = true >
                    <#assign name = name.plural >
                    <#break>
                </#if>
            </#list>
            <#if foundTypeParameter >
                this.${column.field.name} = new ${name}Converter(entity.${column.getter}());
            <#else>
                this.${column.field.name} = entity.${column.getter}();
            </#if>        
        <#else>
            <#if found >
        this.${column.field.name} = new ${column.field.type}Converter(entity.${column.getter}());
            <#else>
        this.${column.field.name} = entity.${column.getter}();
            </#if>
        </#if>
        </#list>
    }

    private ${entity.idColumn.field.type} ${entity.idColumn.field.name};
    <#list entity.columns as column>
    <#list classNames as name>
    <#assign found = false >
    <#if name.single == column.field.type >
        <#assign found = true >
        <#break>
    </#if>
    </#list>
    <#if column.field.hasTypeParameter >
        <#list classNames as name>
            <#assign foundTypeParameter = false >
            <#if name.single == column.field.typeParameter >
                <#assign foundTypeParameter = true >
                <#assign name = name.plural >
                <#break>
            </#if>
        </#list>
        <#if foundTypeParameter >
    private ${name}Converter ${column.field.name};
        <#else>
    private ${column.field.type}<${column.field.typeParameter}> ${column.field.name};
        </#if>
    <#else>
        <#if found >
    private ${column.field.type}Converter ${column.field.name};
        <#else>
    private ${column.field.type} ${column.field.name};
        </#if>
    </#if>
    </#list>

    /**
     * Getter for ${entity.idColumn.field.name}.
     *
     * @return value for ${entity.idColumn.field.name}
     */
    public ${entity.idColumn.field.type} ${entity.idColumn.getter}()
    {
        return this.${entity.idColumn.field.name};
    }

    /**
     * Setter for ${entity.idColumn.field.name}.
     *
     * @param value the value to set
     */
    public void ${entity.idColumn.setter}(${entity.idColumn.field.type} value)
    {
        this.${entity.idColumn.field.name} = value;
    }

    <#list entity.columns as column>
        <#list classNames as name>
            <#assign found = false >
            <#if name.single == column.field.type >
                <#assign found = true >
                <#break>
            </#if>
        </#list>
        <#if column.field.hasTypeParameter >
            <#list classNames as name>
                <#assign foundTypeParameter = false >
                <#if name.single == column.field.typeParameter >
                    <#assign foundTypeParameter = true >
                    <#assign name = name.plural >
                    <#break>
                </#if>
            </#list>
            <#if foundTypeParameter >
    /**
     * Getter for ${column.field.name}.
     *
     * @return value for ${column.field.name}
     */
    public ${column.field.type}<${column.field.typeParameter}> ${column.getter}()
    {
        return (${column.field.type}<${column.field.typeParameter}>) this.${column.field.name}.getEntities();
    }

    /**
     * Setter for ${column.field.name}.
     *
     * @param value the value to set
     */
    public void ${column.setter}(${column.field.type}<${column.field.typeParameter}> value)
    {
        this.${column.field.name} = new ${name}Converter(value);
    }
            <#else>
    /**
     * Getter for ${column.field.name}.
     *
     * @return value for ${column.field.name}
     */
    public ${column.field.type}<${column.field.typeParameter}> ${column.getter}()
    {
        return (${column.field.type}<${column.field.typeParameter}>) this.${column.field.name};
    }

    /**
     * Setter for ${column.field.name}.
     *
     * @param value the value to set
     */
    public void ${column.setter}(${column.field.type}<${column.field.typeParameter}> value)
    {
        this.${column.field.name} = value;
    }
            </#if>
        <#else>
    /**
     * Getter for ${column.field.name}.
     *
     * @return value for ${column.field.name}
     */
    public ${column.field.type} ${column.getter}()
    {
            <#if found >
        return this.${column.field.name}.getEntity();
            <#else>
        return this.${column.field.name};
            </#if>
    }

    /**
     * Setter for ${column.field.name}.
     *
     * @param value the value to set
     */
    public void ${column.setter}(${column.field.type} value)
    {
            <#if found >
        this.${column.field.name} = new ${column.field.type}Converter(value);
            <#else>
        this.${column.field.name} = value;
            </#if>
    }
        </#if>
    </#list>

    public ${entity.name.single} getEntity() {
        ${entity.name.single} entity = new ${entity.name.single}();
        entity.${entity.idColumn.setter}(this.${entity.idColumn.field.name});
        <#list entity.columns as column>
            <#list classNames as name>
                <#assign found = false >
                <#if name.single == column.field.type >
                    <#assign found = true >
                    <#break>
                </#if>
            </#list>
        <#if column.field.hasTypeParameter >
            <#list classNames as name>
                <#assign foundTypeParameter = false >
                <#if name.single == column.field.typeParameter >
                    <#assign foundTypeParameter = true >
                    <#assign name = name.plural >
                    <#break>
                </#if>
            </#list>
            <#if foundTypeParameter >
                entity.${column.setter}(this.${column.field.name} == null? null : (${column.field.type}<${column.field.typeParameter}>)this.${column.field.name}.getEntities());
            <#else>
                entity.${column.setter}((${column.field.type}<${column.field.typeParameter}>)this.${column.field.name});
            </#if>
        <#else>
            <#if found >
        entity.${column.setter}(this.${column.field.name} == null? null:this.${column.field.name}.getEntity());
            <#else>
        entity.${column.setter}(this.${column.field.name});
            </#if>
        </#if>
        </#list>
        return entity;
    }
}
</#list>
